Page({
  data: {
    canIUseAuthButton: my.canIUse('button.open-type.getAuthorize'),
    userInfo: null,
    searchInput: '', // 添加用于搜索的属性
    schools: ["哈尔滨工业大学", "哈尔滨理工大学", "北京大学"],     // 添加用于存储学校列表的属性
    matchedSchools: [], // 添加用于存储匹配搜索条件的学校列表
    scrollPickerData: [], // 添加用于存储滚动选择条数据的属性
    selectedSchool: '',// 添加用于存储当前选择的学校
    showAuthButton: true
  },

  onLoad(){
    this.getOpenUserInfoByAuthCode();
  },

  // 添加处理滚动选择条变化的函数
  onPickerChange(e) {
    const index = e.detail.value;
    const selectedSchool = this.data.scrollPickerData[index];
    console.log("你选择的学校是：", selectedSchool);
    
    this.setData({
      selectedSchool: selectedSchool,
      searchInput: selectedSchool, // 将选择的学校传回搜索框
    });
    this.requestToBackend_school(selectedSchool);
  },
  
  // 添加用于处理搜索输入的函数
  onSearchInput(e) {
    const searchInput = e.detail.value;
    // 实现根据搜索输入在数据库中搜索学校的逻辑
    // 用搜索结果更新 'schools' 和 'matchedSchools' 属性
    const matchedSchools = this.data.schools.filter(school => school.includes(searchInput));
    this.setData({
      searchInput: searchInput,
      matchedSchools: matchedSchools,
      scrollPickerData: matchedSchools, // 更新滚动选择条数据
    });
  },

  getOpenUserInfoByAuthCode() {
    const that = this;
    // my.alert(authCode);
    console.log('zhixingzhong');
    my.getOpenUserInfo({
      fail: (error) => {
        console.error('获取用户信息失败：', error);
        this.setData({
          authText: '',
          userInfo: null,
          showAuthButton: true,
        });
      },
      success: (res) => {
        if (res.response.split(':')[2].split(',')[0].toString() == '"10000"') {
          const userInfo = JSON.parse(res.response).response;
          console.log('用户信息：', res.response);
          that.setData({
            authText: "昵称：" + userInfo.nickName ? "昵称：" + userInfo.nickName : '',
            userInfo: userInfo,
            showAuthButton: false, // 授权成功后隐藏按钮
          });
          this.requestToBackend_personal_information(res.data);
        }
        else{
          const userInfo = JSON.parse(res.response).response;
          console.log('用户信息：', res.response);
          that.setData({
            authText: "昵称：" + userInfo.nickName ? "昵称：" + userInfo.nickName : '',
            userInfo: userInfo,
            showAuthButton: true, // 授权失败后展示按钮
          });
        }
      },
    })
  },

  // 获取手机号码授权
  onPhoneTextTap() {
    const that = this;
    my.getPhoneNumber({
      success: (res) => {
        // 用户点击允许
        const encryptPhoneNumber = res.response.split(':')[1].split(',')[0];
        console.log(encryptPhoneNumber)
        console.log(res.response)
        // 发起请求到后端
        that.requestToBackend_phonenumber(res.response);
      },
      fail: (error) => {
        // 用户点击拒绝
        my.showToast({
          content: '授权失败，请重新尝试',
          type: 'fail'
        });
      },
    });
  },
  
  // 发起请求到后端的函数
  // phonenumber
  requestToBackend_phonenumber(json_data) {
    const that = this;
    // const apiUrl = 'https://localhost:3000/php/1.php'; // 替换成你的后端接口地址
    // const apiUrl = 'http://172.20.55.210:10086/product/getproducts'; // 替换成你的后端接口地址
    const apiUrl = 'http://172.20.55.210:10086/user/decrypt'; // 替换成你的后端接口地址

    console.log(json_data);
    
    my.request({
      url: apiUrl,
      method: 'POST',
      data: json_data,
      headers: {
        'content-type': 'application/json', //默认值
      },
      dataType: 'json',
      success: function (res) {
        console.log(res.data);
        my.alert({ content: 'success' });
      },
      fail: function (error) {
        console.error('请求失败：', error);
        my.alert({ content: '请求失败，详细错误信息请查看控制台' });
      },
      complete: function (res) {
        my.hideLoading();
        my.alert({ content: 'complete' });
      },
    });
  },
  
  // school
  requestToBackend_school(data){
    const that = this;
    // const apiUrl = 'https://localhost:3000/php/1.php'; // 替换成你的后端接口地址
    // const apiUrl = 'http://172.20.55.210:10086/product/getproducts'; // 替换成你的后端接口地址
    const apiUrl = 'http://172.20.55.210:10086/user/school'; // 替换成你的后端接口地址

    my.request({
      url: apiUrl,
      method: 'POST',
      data: {
        'school': data,
      },
      headers: {
        'content-type': 'application/json', //默认值
      },
      dataType: 'json',
      success: function (res) {
        console.log(res.data);
        my.alert({ content: 'success' });
      },
      fail: function (error) {
        console.error('请求失败：', error);
        my.alert({ content: '请求失败，详细错误信息请查看控制台' });
      },
      complete: function (res) {
        my.hideLoading();
        my.alert({ content: 'complete' });
      },
    });
  },

  // personal_information
  requestToBackend_personal_information(data){
    const that = this;
    // const apiUrl = 'https://localhost:3000/php/1.php'; // 替换成你的后端接口地址
    // const apiUrl = 'http://172.20.55.210:10086/product/getproducts'; // 替换成你的后端接口地址
    const apiUrl = 'http://172.20.55.210:10086/user'; // 替换成你的后端接口地址

    my.request({
      url: apiUrl,
      method: 'POST',
      data: data,
      headers: {
        'content-type': 'application/json', //默认值
      },
      dataType: 'json',
      success: function (res) {
        console.log(res.data);
        my.alert({ content: 'success' });
      },
      fail: function (error) {
        console.error('请求失败：', error);
        my.alert({ content: '请求失败，详细错误信息请查看控制台' });
      },
      complete: function (res) {
        my.hideLoading();
        my.alert({ content: 'complete' });
      },
    });
  }

});
